package com.example.uiclassassignments.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Toast;
import com.example.uiclassassignments.R;

@SuppressLint("Registered")
public class DialogActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }

    public void btnOnClick(View view) {
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(DialogActivity.this);
        alertDialog.setIcon(R.drawable.ic_launcher_background);
        alertDialog.setTitle("单选");
        final String[] aryCity = {"北京", "深圳", "上海", "厦门"};

        alertDialog.setItems(aryCity, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"选中了"+aryCity[which],Toast.LENGTH_LONG).show();
            }
        });
        alertDialog.show();
    }

    public void btnOnClick2(View view) {
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(DialogActivity.this);
        alertDialog.setIcon(R.drawable.ic_launcher_background);
        alertDialog.setTitle("单选");
        final String[] aryCity = {"北京", "深圳", "上海", "厦门"};

        alertDialog.setMultiChoiceItems(aryCity, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                StringBuilder sb = new StringBuilder();
                if(isChecked)
                {
                    sb.append(aryCity[which]).append(", ");
                }
                Toast.makeText(DialogActivity.this, "爱好为：" + sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(DialogActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(DialogActivity.this,"取消",Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.show();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
